package it.win4buy.services
{
    import flash.events.IEventDispatcher;

    import it.win4buy.entities.UserDataVO;
    import it.win4buy.events.GetUserDataEvent;
    import it.win4buy.events.UtilityEvent;
    import it.win4buy.mock.RemoteObjectMock;
    import it.win4buy.utility.ApplicationConstants;
    import it.win4buy.utility.LoggerUtilty;

    import mx.controls.Alert;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.remoting.RemoteObject;

    /**
     *
     *	autore:
     *  pierangelo.mancusi@gmail.com
     *
     */

    public class GetUserDataService
    {




        //-------------------------------------------
        //
        // Injected properties
        //
        //-------------------------------------------

        [Dispatcher]
        public var dispatcher:IEventDispatcher;

        [Inject]
        public var log:LoggerUtilty;
        /*
                [Inject]
                public var _ro:RemoteObjectMock;
        */
        private var _ro:RemoteObject;

        //-------------------------------------------
        //
        // Private Properties
        //
        //-------------------------------------------



        //-------------------------------------------
        //
        // Getters / Setters
        //
        //-------------------------------------------


        //-------------------------------------------
        //
        // Constructor
        //
        //-------------------------------------------

        public function GetUserDataService()
        {

        }

        //-------------------------------------------
        //
        // Mediator Handlers
        //
        //-------------------------------------------

        //-------------------------------------------
        //
        // Public Methods
        //
        //-------------------------------------------

        public function execute(userData:UserDataVO):void
        {
            log.logToConsole(this, "retrieving UserData...");

            _ro=new RemoteObject();
            //fixme: remoteObject Destination
            _ro.destination=ApplicationConstants.DESTINATION;
            // _ro.showBusyCursor=true;
            _ro.addEventListener(FaultEvent.FAULT, faultHandler);
            _ro.addEventListener(ResultEvent.RESULT, resultHandler);

            _ro.getUserData(userData.userId);

        }

        //-------------------------------------------
        //
        // Private Methods
        //
        //-------------------------------------------

        private function faultHandler(event:FaultEvent):void
        {
            _ro.showBusyCursor=false;
            _ro.removeEventListener(FaultEvent.FAULT, faultHandler);
            _ro.removeEventListener(ResultEvent.RESULT, resultHandler);


            dispatcher.dispatchEvent(new GetUserDataEvent(GetUserDataEvent.FAULT, "messaggioTest", true));

            _ro=null;
        }

        private function resultHandler(event:ResultEvent):void
        {
            _ro.showBusyCursor=false;
            _ro.removeEventListener(FaultEvent.FAULT, faultHandler);
            _ro.removeEventListener(ResultEvent.RESULT, resultHandler);

            var userDataTemp:UserDataVO=event.result as UserDataVO;

            //fixme: testing purpose
            //send userData to the controller
            //dispatcher.dispatchEvent(new UtilityEvent(UtilityEvent.DATA_READY, userDataTemp, true));

            dispatcher.dispatchEvent(new GetUserDataEvent(GetUserDataEvent.RESULT, userDataTemp, true));

            _ro=null;
        }



    }
}